<template>
  <view class="login-container">
    <view class="login-box">
      <image src="../../static/user.png" class="login-img"></image>
      <button type="primary" open-type="getUserInfo" class="login" @getuserinfo="getUserInfo">一键登录</button>
      <view class="login-text">
        登录后尽享更多权益
      </view>
    </view>
  </view>
</template>

<script>
  import {mapState,mapMutations} from 'vuex'
  export default {
    name:"my-login",
    computed:{
      ...mapState('m_user',['token','redirectInfo'])
    },
    methods:{
      ...mapMutations('m_user',['updateUserInfo','updateToken','updateRedirectInfo']),
      getUserInfo(e){
        if(e.detail.errMsg==='getUserInfo:fail auth deny')return uni.$showMsg('你取消了授权！')
        this.updateUserInfo(e.detail.userInfo)
        // console.log(e)
        this.getToken(e.detail)
      },
      async getToken(info){
        const [err,res]=await uni.login().catch(err=>err)
        if(err||res.errMsg!=='login:ok')return uni.$showMsg('登录失败！')
        // console.log(res)
        // console.log(info)
        const query={
          code:res.code,
          encryptedData:info.encryptedData,
          iv:info.iv,
          rawData:info.rawData,
          signature:info.signature
        }
        const {data}=await uni.$http.post('/api/public/v1/users/wxlogin',query)
        // console.log(data)
        this.updateToken('Bearer xxx')
        // if(data.meta.status!==200)return uni.$showMsg('登录失败！')
        // uni.$showMsg('登录成功！')
        this.navigateBack()
      },
      navigateBack(){
        if(this.redirectInfo&&this.redirectInfo.openType==='switchTab'){
          uni.switchTab({
            url:this.redirectInfo.from,
            complete: () => {
              this.updateRedirectInfo(null)
            }
          })
        }
      }
    }
  }
</script>

<style lang="scss">
.login-container{
  overflow: hidden;
  background-color: #fff;
}
.login-box{
  position: relative;
  text-align: center;
  height: 330px;
  background-color: #eee;
  .login-img{
    margin-top: 50px;
    width: 128px;
    height: 128px;
  }
  .login{
    width: 92%;
    background-color: #E16531;
    color: #fff;
    line-height: 50px;
    border-radius: 25px;
    text-align: center;
    margin: 24px auto;
  }
  .login-text{
    font-size: 12px;
    color: #aaa;
  }
  &::after{
    content: '';
    display: block;
    width: 100%;
    height: 20px;
    border-radius: 100%;
    background-color: #fff;
    transform: translateY(50%);
    position: absolute;
    bottom: 0;
    left: 0;
  }
}
</style>
